###################################
### SSA/ASS converter functions ###
###################################

def ssa2ssa(file_in, file_out):
	sub_ssa = ssa()
	sub_ssa.load(file_in)
	print("Mode: SSA/ASS >> SSA/ASS")
	sub_ssa.save(file_out);

def ssa2sbv(file_in, file_out):
	sub_sbv = sbv()
	sub_ssa = ssa()
	sub_ssa.load(file_in)
	print("Mode: SSA/ASS >> SBV")
	for ssa_event in sub_ssa.events:
		if (ssa_event.event_type == "Dialogue"):
			sbv_evt = sbv_event()
			sbv_evt.time_start = ssa_event.time_start
			sbv_evt.time_end = ssa_event.time_end
			# remove soft linebreaks \n, hardspaces \h, override blocks {\*} and split the text on hard linebreaks (\N)
			sbv_evt.text = map(str.strip, re.sub(r"\{\\.*?\}|\\n|\\h", "", ssa_event.content).split("\\N"))
			sub_sbv.merge_event(sbv_evt)
	sub_sbv.save(file_out);

def ssa2srt(file_in, file_out):
	sub_srt = srt()
	sub_ssa = ssa()
	sub_ssa.load(file_in)
	print("Mode: SSA/ASS >> SRT")
	for ssa_event in sub_ssa.events:
		if (ssa_event.event_type == "Dialogue"):
			srt_evt = srt_event()
			srt_evt.time_start = ssa_event.time_start
			srt_evt.time_end = ssa_event.time_end
			# remove soft linebreaks \n, hardspaces \h, override blocks {\*} and split the text on hard linebreaks (\N)
			srt_evt.text = map(str.strip, re.sub(r"\{\\.*?\}|\\n|\\h", "", ssa_event.content).split("\\N"))
			# currently only SRT v1 (not advanced) support
			# TODO: instead of removing override blocks, extract supported override tags and convert them to SRT advanced html tags
			# also do a lookup in style section for formating and create SRT advanced html tags
			# ...
			sub_srt.merge_event(srt_evt)
	sub_srt.save(file_out);

###############################
### SRT converter functions ###
###############################

def srt2srt(file_in, file_out):
	sub_srt = srt()
	sub_srt.load(file_in)
	print("Mode: SRT >> SRT")
	sub_srt.save(file_out);

def srt2sbv(file_in, file_out):
	sub_sbv = sbv()
	sub_srt = srt()
	sub_srt.load(file_in)
	print("Mode: SRT >> SBV")
	for evt_srt in sub_srt.events:
		evt_sbv = sbv_event()
		evt_sbv.text = []
		evt_sbv.time_start = evt_srt.time_start.replace(",", ".")
		evt_sbv.time_end = evt_srt.time_end.replace(",", ".")
		for line in evt_srt.text:
			line = re.sub(r"\{\\.*?\}", "", line) # override blocks {\*}
			line = re.sub(r"</?(b|i|u|s|font.*?)>", "", line) # remove common tags
			evt_sbv.text.append(line)
		sub_sbv.events.append(evt_sbv)
	sub_sbv.save(file_out);

def srt2ssa(file_in, file_out):
	sub_ssa = ssa()
	sub_srt = srt()
	sub_srt.load(file_in)
	print("Mode: SRT >> SSA/ASS")
	print("Conversion not supported...")
	sub_ssa.save(file_out);

###############################
### SBV converter functions ###
###############################

def sbv2sbv(file_in, file_out):
	sub_sbv = sbv()
	sub_sbv.load(file_in)
	print("Mode: SBV >> SBV")
	sub_sbv.save(file_out);

def sbv2srt(file_in, file_out):
	sub_srt = srt()
	sub_sbv = sbv()
	sub_sbv.load(file_in)
	print("Mode: SBV >> SRT")
	for evt_sbv in sub_sbv.events:
		evt_srt = srt_event()
		evt_srt.text = []
		evt_srt.time_start = evt_sbv.time_start.replace(".", ",")
		evt_srt.time_end = evt_sbv.time_end.replace(".", ",")
		evt_srt.text = evt_sbv.text
		sub_srt.events.append(evt_srt)
	sub_srt.save(file_out);

def sbv2ssa(file_in, file_out):
	sub_ssa = ssa()
	sub_sbv = sbv()
	sub_sbv.load(file_in)
	print("Mode: SBV >> SSA/ASS")
	print("Conversion not supported...")
	sub_ssa.save(file_out);				
